<template>
  <view class="viewport">
    <!-- 封面图 -->
    <view class="cover">
      <image class="image" mode="widthFix" :src="subject?.image"></image>
    </view>
    <!-- 商品列表 -->
    <scroll-view enable-back-to-top :key="subject?.subjectId" scroll-y class="scroll-view">
      <view class="goods">
        <navigator
          hover-class="none"
          class="navigator"
          v-for="goods in subject?.subjectItemSaleList"
          :key="goods.itemSaleCode"
          :url="`/pages/goods/goods?id=${goods.itemSaleId}`"
        >
          <image class="thumb" :src="getMainImage(goods.mainImage)"></image>
          <view class="name ellipsis">{{ goods.itemSaleName }}</view>
          <view class="price">
            <text class="symbol">¥</text>
            <text class="number">{{ goods.itemSalePrice }}</text>
          </view>
        </navigator>
      </view>
    </scroll-view>
  </view>
</template>

<script setup lang="ts">
import { selectSubjectById } from '@/services/subject'
import type { Subject } from '@/types/subject'
import { onLoad } from '@dcloudio/uni-app'
import { ref, defineProps, computed } from 'vue'

// uniapp 获取页面参数
const query = defineProps<{ subjectId: number }>()
let subject = ref<Subject>()
// 获取热门推荐数据
const getSubjectData = async () => {
  const res = await selectSubjectById(query.subjectId)
  subject.value = res.data
  uni.setNavigationBarTitle({ title: subject.value.title })
}

// 首图
const getMainImage = computed(() => (mainImage: string) => {
  console.log('casd', mainImage)
  return mainImage.split(',')[0]
})

// 页面加载
onLoad(() => {
  getSubjectData()
})
</script>

<style lang="scss">
page {
  height: 100%;
  background-color: #f4f4f4;
}
.viewport {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 180rpx 0 0;
  position: relative;
}
.cover {
  width: 750rpx;
  height: 225rpx;
  border-radius: 0 0 40rpx 40rpx;
  overflow: hidden;
  position: absolute;
  left: 0;
  top: 0;
  .image {
    width: 750rpx;
  }
}
.scroll-view {
  flex: 1;
}
.tabs {
  display: flex;
  justify-content: space-evenly;
  height: 100rpx;
  line-height: 90rpx;
  margin: 0 20rpx;
  font-size: 28rpx;
  border-radius: 10rpx;
  box-shadow: 0 4rpx 5rpx rgba(200, 200, 200, 0.3);
  color: #333;
  background-color: #fff;
  position: relative;
  z-index: 9;
  .text {
    margin: 0 20rpx;
    position: relative;
  }
  .active {
    &::after {
      content: '';
      width: 40rpx;
      height: 4rpx;
      transform: translate(-50%);
      background-color: #4dc178;
      position: absolute;
      left: 50%;
      bottom: 24rpx;
    }
  }
}
.goods {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 20rpx 20rpx;
  .navigator {
    width: 345rpx;
    padding: 20rpx;
    margin-top: 20rpx;
    border-radius: 10rpx;
    background-color: #fff;
  }
  .thumb {
    width: 305rpx;
    height: 305rpx;
  }
  .name {
    height: 88rpx;
    font-size: 26rpx;
  }
  .price {
    line-height: 1;
    color: #cf4444;
    font-size: 30rpx;
  }
  .symbol {
    font-size: 70%;
  }
  .decimal {
    font-size: 70%;
  }
}

.loading-text {
  text-align: center;
  font-size: 28rpx;
  color: #666;
  padding: 20rpx 0 50rpx;
}
</style>
